<?php

namespace App\Http\Resources\panyuan;

use App\model\panyuan\MyApply;
use App\model\panyuan\UserShenheLog;
use App\model\Workshop;
use App\UserShenhe;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;

class UserShenheResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        if ($this->workshop->type == 0) {
            $price = $this->workshop->rent != 0 ? $this->workshop->rent . Workshop::RENT : '面议';
        } else {
            $price = $this->workshop->price != 0 ? $this->workshop->rent . Workshop::SALE : '面议';
        }
        $status=MyApply::where('code',$this->code)->first();
        if ($status->status==MyApply::SHENHE_REFUSE){
            $refuseReason=UserShenhe::where('status',UserShenhe::STATUS_JUJUE)->orderBy('id','desc')->first()->refuseReason;

        }else{
            $refuseReason=$this->refuseReason;
        }
        return [
            'code'=>'success',
            'message'=>'请求成功',
            'data'=>[
                'id'=>$this->id,
                'code'=>$this->code,
                'content'=>UserShenhe::$typeMap[$this->type],
                'status'=>$status->status,
                'my_status'=>$this->status,
                'status_type'=> MyApply::$statusMap[$status->last_status] ?? '',
                'refuseReason'=>$refuseReason ?? '',
                'user'=>$this->user,
                'type'=>$this->type,
                'workshop'=>[
                  'id'=>$this->workshop->id,
                  'title'=>$this->workshop->title,
                  'price'=>$price,
                   'area'=>$this->workshop->area,
                  'industry'=>$this->workshop->industry->reject(function($item,$key){
                      return $key>1;
                  }),
                    'tags'=>$this->workshop->tags->reject(function($item,$key){
                        return $key>2;
                    }),
                    'logo'=>$this->workshop->logo

                ],
                'log'=>$this->log->map(function ($log){
                    return [
                        'user'=>$log->user,
                        'created_at'=>$log->created_at,
                        'status'=>$log->status,
                        'status_desc'=>UserShenheLog::$statusMap[$log->status],
                        'created_at'=>Carbon::parse($log->created_at)->format('Y-m-d H:i'),
                        'role_id'=>$log->role_id,
                        'role_name'=>$log->role->name ?? ''
                    ];
                }),
                'created_at'=>Carbon::parse($this->created_at)->format('Y-m-d H:i')
            ]
        ];
    }
}
